package com.atguigu.springcloud.controller;

import com.atguigu.springcloud.config.Result;
import com.atguigu.springcloud.entities.Payment;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * 订单系统 前端控制器
 *
 * @author zhenyu
 * @since 2022-08-29
 */
@RestController
@Api(value = "订单管理", tags = "订单管理")
@RequestMapping("/consumer/payment")
@Slf4j
public class OrderController {

  //public static final String PAYMENT_URL = "http://localhost:8001";
  public static final String PAYMENT_URL = "http://CLOUD-PAYMENT-SERVICE"; //eureka注册的服务名称

  @Resource private RestTemplate restTemplate;

  @PostMapping("/create")
  @ApiOperation("新增订单记录")
  public Result<Payment> create(@RequestBody Payment payment) {
    return restTemplate.postForObject(PAYMENT_URL + "/payment/create", payment, Result.class);
  }

  @GetMapping("/getPaymentById/{id}")
  @ApiOperation("查询订单记录")
  @ApiImplicitParam(value = "id", name = "id", dataType = "Long")
  public Result<Payment> getPaymentById(@PathVariable("id") Long id) {
    return restTemplate.getForObject(
            PAYMENT_URL + "/payment/getPaymentById/"+id, Result.class);
  }
}
